package com.hxzy.dao.impl;

import com.hxzy.dao.ActorDao;
import com.hxzy.util.DbHelper;
import entity.Actor;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.util.Optional;

public class ActorDaoImpl extends DbHelper  implements ActorDao {
    @Override
    public int insert(Actor actor) {
        String sql="insert into actor (actor_name) values(?)";
        Object[] objects={
                actor.getActorName(),
        };
        return super.executeUpdate(sql,objects);
    }

    @Override
    public int updateByPrimaryKey(Actor actor) {
        String sql="update  actor set actor_name=?  where actor_id=?";
        Object[] objects={
                actor.getActorName(),
                actor.getActorId(),
        };
        return super.executeUpdate(sql,objects);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        String sql="delete from actor where actor_id=?";
        return super.executeUpdate(sql,id);
    }

    @Override
    public Optional<Actor> selectByPrimaryKey(Integer id) {
        String sql="select  actor_name as  actorName,  actor_id as actorId from actor where actor_id=? ";
        ResultSetHandler<Actor> handler = new BeanHandler<>(Actor.class);
        Optional<Actor> optional = super.executeQuery(sql, handler, id);
        return optional;
    }
}
